package com.woniuxy.cloud.procurementassembly.controller;


import com.woniuxy.cloud.demo.Result;
import com.woniuxy.cloud.procurementassembly.dto.BatteryOrderDto;
import com.woniuxy.cloud.procurementassembly.exception.ProcurementException;
import com.woniuxy.cloud.procurementassembly.param.ReceiptParam;
import com.woniuxy.cloud.procurementassembly.param.SaveBatteryOrderParam;
import com.woniuxy.cloud.procurementassembly.param.UpdateBatteryOrderStatueParam;
import com.woniuxy.cloud.procurementassembly.service.BatteryOrderService;
import com.woniuxy.cloud.procurementassembly.util.ValidatedUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 周川雄
 * @since 2022-02-18
 */
@RestController
@RequestMapping("/battery-order")
public class BatteryOrderController {

    @Resource
    private BatteryOrderService batteryOrderService;

    /**
     * 发布采购订单
     * @param param
     * @param result 返回订单详情
     * @return
     */
    @ApiOperation("发布采购订单")
    @PostMapping("/SaveBatteryOrder.do")
    public Result<BatteryOrderDto> SaveBatteryOrder(@RequestBody @Validated SaveBatteryOrderParam param, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);

        BatteryOrderDto dto = batteryOrderService.SaveBatteryOrder(param);
        if (!StringUtils.isEmpty(dto)) {
            return Result.newInstance().setCode(200).setMsg("添加成功").setData(dto);
        }
        throw new ProcurementException("发布采购订单失败", 502);

    }


    @ApiOperation("审批采购订单")
    @PostMapping("/UpdateBatteryOrderStatue.do")
    public Result<BatteryOrderDto> UpdateBatteryOrderStatue(@RequestBody @Validated UpdateBatteryOrderStatueParam param, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);

        if (batteryOrderService.UpdateBatteryOrderStatue(param)) {
            return Result.newInstance().setCode(200).setMsg("审批成功");
        }
        throw new ProcurementException("审批采购订单失败", 502);

    }


    @ApiOperation("查询采购订单")
    @PostMapping("/FindBatteryOrderByOrderId.do")
    public Result<BatteryOrderDto> FindBatteryOrderByOrderId(@RequestBody Integer orderId) {

        BatteryOrderDto dto = batteryOrderService.FindBatteryOrderByOrderId(orderId);
        return Result.newInstance().setCode(200).setMsg("查询订单成功").setData(dto);
    }


    @ApiOperation("确认收货 可以只收货一部分,但是不收货")
    @PostMapping("/ReceiptOrder.do")
    public Result<BatteryOrderDto> Receipt(@RequestBody @Validated ReceiptParam param, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);

        BatteryOrderDto dto = batteryOrderService.ReceiptParam(param);
        if (!StringUtils.isEmpty(dto)) {
            return Result.newInstance().setCode(200).setMsg("添加成功").setData(dto);
        }
        throw new ProcurementException("发布采购订单失败", 502);

    }

}

